<?php
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/HumeurDatabase.class.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/Member.class.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/GaleryItem.interface.php");
	
	class Account implements GaleryItem{
		var $mail;
		var $password;
		var $isConfirmed;
		var $isAdmin;
		var $members;
		
		public function Account($data=NULL){
			$this->members = array();
			if(is_array($data)) $this->create($data);
			elseif(is_string($data)) $this->load($data);
		}
		
		public function load($mail){
			$db = new HumeurDatabase();
			$data = mysql_fetch_assoc($db->query("SELECT * FROM accounts WHERE mail = '".$mail."' LIMIT 1;"));
			$this->create($data);
			$this->loadMembers();
		}
		
		public function loadMembers(){
			$db = new HumeurDatabase();
			$this->members = array();
			$data = $db->query("SELECT memberId FROM members WHERE accountMail = '".$this->mail."' ORDER BY memberId ASC;");
			while($member = mysql_fetch_assoc($data)){
				$this->members[$member['memberId']] = new Member($member['memberId']);
			}
		}
		
		public function create($array){
			$this->mail = (isset($array['mail']))? $array['mail']:NULL;
			$this->password = (isset($array['password']))? $array['password']:NULL;
			$this->isConfirmed = (isset($array['isConfirmed']))? $array['isConfirmed']:0;
			$this->isAdmin = (isset($array['isAdmin']))? $array['isAdmin']:0;
		}
		
		public function save(){
			if($this->isConfirmed == 0){
				$this->password = md5($this->password);
				if(!$this->insert()) return false;
			}else{
				if(!$this->update()){return false;}
			}
			foreach($this->members as $member){
				if(!$member->save()) return false;
			}
			$this->loadMembers();
			return true;
		}
		
		public function insert(){
			$db = new HumeurDatabase();
			if(mysql_num_rows($db->query("SELECT * FROM accounts WHERE mail = '".$this->mail."';")) > 0) return false;
			return $db->query("INSERT INTO accounts VALUES ('".$this->mail."','".$this->password."','".$this->isConfirmed."','".$this->isAdmin."');");
		}
		
		public function update(){
			$db = new HumeurDatabase();
			return $db->query("UPDATE accounts SET password = '".$this->password."', isConfirmed = '".$this->isConfirmed."',isAdmin = '".$this->isAdmin."' WHERE mail = '".$this->mail."';");
		}
		
		public function confirm($newPass){
			$this->isConfirmed = 1;
			$this->password = md5($newPass);
			return ($this->update())? 1:0;
		}
		
		public function login($login,$pass){
			$db = new HumeurDatabase();
			$data = $db->query("SELECT * FROM accounts WHERE mail = '".$login."' AND password = '".md5($pass)."' LIMIT 1;");
			if(mysql_num_rows($data) == 0) return -1;
			$this->load($login);
			$_SESSION['account'] = $this;
			return 1;
		}
		
		public function addMember($data){
			$data['accountMail'] = $this->mail;
			$this->members[] = new Member($data);
		}
		
		public function deleteMember($id){
			if($this->members[$id]->delete()){
				unset($this->members[$id]);
				return true;
			}
			return false;
		}
		
		public function toggleAdmin(){
			$this->isAdmin = ($this->isAdmin == 1)? 0:1;
			return $this->isAdmin;
		}
		
		public function delete(){
			$db = new HumeurDatabase();
			foreach($this->members as $member){
				if(!$member->delete()) return false;
			}
			return $db->query("DELETE FROM accounts WHERE mail = '".$this->mail."';");
		}
		
		public function summary(){
			return '<div id="'.$this->mail.'" class="existingColorBloc centered"><div>'.$this->mail.'</div><br/><div><img onclick="ajaxLoad(\'disableAdmin\',\'mail='.$this->mail.'\',disableAdmin_response)" src="img/isNotAdmin.png" alt="Droit d\'administrateur" /></div></div>';
		}
	}
?>